package UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class client {
    private DatagramSocket socket=null;
    private String ipClient;
    private int portServer;

    public client(String ip, int port) throws SocketException {
        socket=new DatagramSocket();
        ipClient=ip;
        portServer=port;
    }
    public void start() throws IOException {
        System.out.println("Client is Running...");
        Scanner scanner=new Scanner(System.in);
        while(true){
            System.out.print("Enter:>");
            String request=scanner.next();
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length, InetAddress.getByName(ipClient),portServer);
            socket.send(requestPacket);
            DatagramPacket responsePacket=new DatagramPacket(new byte[4090],4090);
            socket.receive(responsePacket);
            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        client client=new client("127.0.0.1",9090);
        client.start();
    }
}
